package com.silence.qhyh.webpro.mvp.ui.activity.login;

import android.annotation.SuppressLint;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.silence.qhyh.webpro.R;
import com.silence.qhyh.webpro.app.widget.verificationcode.MessageContentObserver;
import com.silence.qhyh.webpro.app.widget.verificationcode.VerificationCodeEditText;
import com.silence.qhyh.webpro.mvp.ui.activity.MainActivity;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.RxDataTool;
import com.vondear.rxui.view.RxTitle;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author silence
 * 验证码
 */
public class VerificationCodeActivity extends BaseActivity {

    @BindView(R.id.rx_title)
    RxTitle rxTitle;
    @BindView(R.id.tv_phone_tip)
    TextView tvPhoneTip;
    @BindView(R.id.tv_count)
    TextView tvCount;
    @BindView(R.id.iv_next)
    CheckBox ivNext;
    @BindView(R.id.vc_code)
    VerificationCodeEditText vcCode;

    public static final int MSG_RECEIVE_CODE = 1; //收到短信的验证码
    private MessageContentObserver messageContentObserver;    //回调接口

    @SuppressLint("HandlerLeak")
    Handler handlerSms = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == MSG_RECEIVE_CODE) {
                //设置读取到的内容
                vcCode.setText(msg.getData().getString("code"));
            }
        }
    };

    private int count = 60;//倒计时计数

    private String phone = "";

    Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            count--;
            tvCount.setText(new StringBuilder().append(count).append(getResources().getString(R.string.app_login6)).toString());
            if (count == 0) {
                tvCount.setText(getResources().getString(R.string.app_login9));
                tvCount.setTextColor(ContextCompat.getColor(VerificationCodeActivity.this, R.color.color_5890DC));
                count = 60;
                return;
            }
            handler.postDelayed(this, 1000);
        }
    };

    Handler handlerCode = new Handler();
    Runnable runnableCode = new Runnable() {
        @Override
        public void run() {
            if (vcCode.getText().toString().length() == 4) {
                ivNext.setChecked(true);
            } else {
                ivNext.setChecked(false);
            }
            handlerCode.postDelayed(this, 1000);
        }
    };

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {

    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_verification_code;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        rxTitle.setLeftFinish(this);
        Bundle bundle = getIntent().getExtras();
        phone = bundle.getString("phone");
        tvPhoneTip.setText(new StringBuilder().append("我们向您").append(RxDataTool.hideMobilePhone4(phone)).append("手机号发送了验证码").toString());
        handler.postDelayed(runnable, 1000);
        handlerCode.postDelayed(runnableCode, 1000);

        messageContentObserver = new MessageContentObserver(VerificationCodeActivity.this, handlerSms);
        getContentResolver().registerContentObserver(Uri.parse("content://sms/"), true, messageContentObserver);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacks(runnable);
        handlerCode.removeCallbacks(runnableCode);
        getContentResolver().unregisterContentObserver(messageContentObserver);
    }

    @OnClick({R.id.tv_count, R.id.iv_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_count:
                tvCount.setTextColor(ContextCompat.getColor(VerificationCodeActivity.this, R.color.color_999999));
                handler.postDelayed(runnable, 1000);
                break;
            case R.id.iv_next:
                RxActivityTool.skipActivity(this, MainActivity.class);
                break;
            default:
                break;
        }
    }
}
